package mail;

import java.io.UnsupportedEncodingException;
import java.net.PasswordAuthentication;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.*;

public class MailBox {

	public static void sendmail(String smtpServer,String port, String toMail,String from,
			String psw, String subject, String body) throws UnsupportedEncodingException, MessagingException{
		Properties properties = new Properties();
		properties.put("mail.smtp.host", smtpServer);
		properties.put("mail.smtp.port", "25");
		properties.put("mail.smtp.starttls.enable", "true");
		properties.put("", "true");
		final String login = from;
		final String pwd = psw;
		Authenticator pa = null;
		if(login != null && pwd !=null){
			properties.put("mail.smtp.auth", "true");
			pa = new Authenticator() {
				public javax.mail.PasswordAuthentication getPasswordAuthentication(){
					return new javax.mail.PasswordAuthentication(login, pwd);
					
				}
				
			};
		}
		Session session = Session.getInstance(properties,pa);
		Message msg = new MimeMessage(session);
		
		msg.setFrom(new InternetAddress(from));
		msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toMail, false));
		msg.setSubject(subject);
		msg.setContent(body, "text/html;charset=utf-8");
		msg.setHeader("X-Mailer", "LotonEmail");
		msg.setSentDate(new Date());
		msg.saveChanges();
		
		Transport.send(msg);
		System.out.println("Mail sended");
	}
	
}
